package com.authine.cloudpivot.engine.service.bizrule.handler;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import com.authine.cloudpivot.engine.component.query.api.FilterExpression;
import com.authine.cloudpivot.engine.component.query.api.FilterExpression.Op;
import com.authine.cloudpivot.engine.component.query.api.helper.Q;
import com.authine.cloudpivot.engine.domain.runtime.BizObject;
import com.authine.cloudpivot.engine.open.domain.bizmodel.BizObjectQuery;
import com.authine.cloudpivot.engine.open.service.OpenEngine;
import com.authine.cloudpivot.engine.service.bizrule.handler.context.Request;

public class 水富案例 extends BaseHandler {

    @SuppressWarnings("unchecked")
    @Override
    public Object handle(Request request) {
        /* 获取系统开放引擎 */
        OpenEngine engine = this.getOpenEngine(request);
        /* 获取当前数据 */
        BizObject bizObject = request.getData().getBizObject();
        /* 获取子表数据 */
        List<Map<String, Object>> childs = (List<Map<String, Object>>) bizObject.getData().get("apply_person_inside");
        /* 获取满足条件的数据 */
        childs = childs.stream().filter(m -> m.get("examine_result").equals("通过")).collect(Collectors.toList());
        /* 根据字段进行分组 */
        Map<String, List<Map<String, Object>>> childsByGroup = childs.stream()
                .collect(Collectors.groupingBy(m -> ((Map<?, ?>) m.get("relevance_supplement")).get("id").toString()));
        /* 没有满足条件的数据直接返回 */
        if (childsByGroup == null || childsByGroup.size() <= 0) {
            return bizObject;
        }
        /* 循环处理数据 */
        for (Entry<String, List<Map<String, Object>>> entry : childsByGroup.entrySet()) {
            /* 获取人员ID集合 */
            String ids = entry.getValue()
                    .stream()
                    .map(u -> ((Map<?, ?>) u.get("relevance_internal_recruitme")).get("id").toString())
                    .distinct()
                    .collect(Collectors.joining(";"));
            /* 查询目标数据 */
            List<BizObject> targets = engine.listBizObjects(new BizObjectQuery() {
                private static final long serialVersionUID = 1L;

                @Override
                public String getSchemaCode() {
                    return "supplement_apply";
                }

                @Override
                public FilterExpression getFilterExpression() {
                    return Q.it("id", Op.Eq, entry.getKey());
                }
            });
            for (BizObject target : targets) {
                /* 获取目标人员列表数据 */
                Map<String, Object> relevanceUsers = (Map<String, Object>) target.getData().get("relevance_internal_recruitme");
                /* 更新目标数据 */
                if (relevanceUsers == null) {
                    target.getData().put("relevance_internal_recruitme", ids);
                } else {
                    String relevanceIds = ids + ";" + relevanceUsers.get("id").toString();
                    target.getData().put("relevance_internal_recruitme", relevanceIds);
                }
                engine.updateBizObject(target, false);
            }
        }
        return bizObject;
    }
}
